package top.igotcha.chapter.chapter8.semaphore;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;

/**
 * @author Gotcha
 * @date 2021/3/19
 * @describe
 */
@Slf4j(topic = "SemaphoreTest01")
public class SemaphoreTest01 {
    public static void main(String[] args) {
        //1.创建信号量
        Semaphore semaphore = new Semaphore(3);
        //2.十个线程执行
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    log.debug("线程开始运行");
                    Thread.sleep(1000);
                    log.debug("线程结束运行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }).start();
        }
    }
}
